package external_libraries;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.Serializable;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * Class ImagePanel The class that defines each panel with the referred image.
 *
 */
public class ImagePanel extends JPanel implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Image img;
    private String o;
    private String currentDir = new File("").getAbsolutePath();

    /**
     * Constructor of a new panel with the referred image.
     *
     * @param img the path of the image.
     */
    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage(), img);
        o = img;
    }

    /**
     * Constructor of a new panel with the referred image.
     *
     * @param img the image that the panel will get.
     */
    public ImagePanel(Image img, String imgS) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));

        if (!imgS.equals(currentDir
                + "\\images\\db_images\\database_background.png")) {
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setOpaque(false);
        }

        setSize(size);
        setLayout(null);
    }

    /**
     * Draw of the image.
     */
    public void paintComponent(Graphics g) {
        if (o.equals(currentDir
                + "\\images\\db_images\\database_background.png")) {
            g.drawImage(img, (this.getWidth() / 2) - (img.getWidth(null) / 2),
                    (this.getHeight() / 2) - (img.getHeight(null) / 2), null);
        } else {
            g.drawImage(img, 0, 0, null);
        }
    }
}
